package com.test.listview;

import java.lang.ref.WeakReference;

import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.Context;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CursorAdapterActivity extends Activity implements OnItemClickListener{
	/** Called when the activity is first created. */

	private ListView mListView;
	private ContactItemListAdapter mAdapter;
	private QueryHandler mQueryHandler;
	// 2.1及以下版本无sort_key字段
	static final String SORT_KEY_COLUMN_NAME = Build.VERSION.SDK_INT > 7 ? Contacts.SORT_KEY_PRIMARY : Contacts.DISPLAY_NAME;
	static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, // 0
			Contacts.DISPLAY_NAME, // 1
			SORT_KEY_COLUMN_NAME, // 2
			Contacts.PHOTO_ID, // 3
			Contacts.STARRED, // 4
			Contacts.TIMES_CONTACTED, // 5
			Contacts.CONTACT_PRESENCE, // 6
			Contacts.LOOKUP_KEY, // 7
			Contacts.HAS_PHONE_NUMBER // 8
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cursor_adapter);
		findAndSettings();
	}

	/**
	 * 查找和设置
	 */
	private void findAndSettings() {
		mListView = (ListView) findViewById(R.id.listview);
		mAdapter = new ContactItemListAdapter(this);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(this);
		mQueryHandler = new QueryHandler(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		startQuery();
	}

	private class ContactItemListAdapter extends CursorAdapter{

		private Context mContext;
		private LayoutInflater mInflater;

		public ContactItemListAdapter(Context context) {
			super(context, null, false);
			mContext = context;
			mInflater = LayoutInflater.from(context);
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			System.out.println("newView() position==" + cursor.getPosition());
			return mInflater.inflate(R.layout.cursor_list_item, null);
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			System.out.println("bindView() position==" + cursor.getPosition());
			TextView imgTextView = (TextView)view.findViewById(R.id.iv_avatar);
			imgTextView.setBackgroundResource(R.drawable.ic_contact_picture);

			TextView nameTextView = (TextView)view.findViewById(R.id.tv_name);
			String name = cursor.getString(1);
			nameTextView.setText(name);
		}



		@Override
		public int getCount() {
			return super.getCount();
		}

		@Override
		public void changeCursor(Cursor cursor) {
			super.changeCursor(cursor);
		}
	}

	void startQuery() {
		mQueryHandler.startQuery(0, null, Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, null, null, null);
	}

	private static class QueryHandler extends AsyncQueryHandler {
		protected final WeakReference<CursorAdapterActivity> mActivity;

		public QueryHandler(Context context) {
			super(context.getContentResolver());
			mActivity = new WeakReference<CursorAdapterActivity>((CursorAdapterActivity) context);
		}

		@Override
		protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
			final CursorAdapterActivity activity = mActivity.get();
			if (activity != null && !activity.isFinishing()) {
				activity.mAdapter.changeCursor(cursor);
			} else {
				if (cursor != null) {
					cursor.close();
				}
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Cursor c = (Cursor) mAdapter.getItem(position);
		String name = c.getString(1);
		Toast toast = Toast.makeText(this, "点击了联系人：" + name, Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.TOP, 100, 100);
		toast.show();
	}
}